package zut.wework.utils;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: hanyunfei
 * @Description: HttpClient工具类，用于发送请求
 * @Date: 2017/10/12
 * @Modified By:
 */
@Component
public class HttpClientUtil {

    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    public static String METHOD_GET = "Get";

    public static String METHOD_POST = "Post";

    private static StringRedisTemplate stringRedisTemplate;

    public static void setStringRedisTemplate(StringRedisTemplate template) {
        stringRedisTemplate = template;
    }


    /**
     * 执行企业微信接口方法
     * @param key AccessToken的key值
     * @param url 接口地址
     * @param json json数据
     * @param method 请求方式 Get or Post
     * @return
     */
    public static Map<String, Object> wxRequest(String key, String url, String json, String method){
        Map<String, Object> mapData = new HashMap<>();
            int num = 0;
            boolean flag = false;
            while(num < 3){
                try{
                    num++;
                    if(stringRedisTemplate.hasKey(key)){
                        url = url.replace(key, stringRedisTemplate.opsForValue().get(key));
                        String jsonData = null;
                        if(method.equals(METHOD_GET)){
                            jsonData = get(url);
                        }else if(method.equals(METHOD_POST)){
                            jsonData = post(url, json);
                        }
                        mapData = JsonUtil.toMap(jsonData);
                        int errCode = Integer.parseInt(mapData.get("errcode").toString());

                        if(errCode == 0){//0：正常
                            return mapData;
                        }else if(errCode == 42001){ // access_token过期
                            stringRedisTemplate.delete(key); //删除key
                            logger.info("key:{},access token过期", key);
                        }else if(errCode == 60111 || errCode == 60104 || errCode == 40029){
                            /**
                             * 60111：userid not found
                             * 60104：mobile existed
                             * 40029: invalid code
                             */
                            logger.info("key:{},url:{},json:{},method:{},msg:{}", key, url, json, method, jsonData);
                            return mapData;
                        }else{
                            flag = true;
                            logger.info("key:{},url:{},json:{},method:{},msg:{}", key, url, json, method, jsonData);
                        }
                    }
                    if(flag == false){
//                        mapData = WXAPIUtil.getAccessToken(key);
                        if(mapData != null){
                            stringRedisTemplate.opsForValue().set(key, mapData.get("access_token").toString(),
                                    Integer.parseInt(mapData.get("expires_in").toString()), TimeUnit.SECONDS);
                        }
                    }
                }catch (Exception ex){
                    ex.printStackTrace();
                    logger.error("wxRequest error:{}", ex.getMessage());
                    logger.error("wxRequest key:{},url:{},json:{}.method:{}", key, url, json, method);
                }
            }

        mapData.put("errcode", -1000);
        mapData.put("errmsg", "未获取到信息");
        return mapData;
    }

    public static String get(String url){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(30 * 1000)   //设置连接超时时间
                .setConnectionRequestTimeout(30 * 1000) // 设置请求超时时间
                .setSocketTimeout(30 * 1000)
                .setRedirectsEnabled(true)//默认允许自动重定向
                .build();

        // get method
        HttpGet httpGet = new HttpGet(url);
        //response
        HttpResponse response = null;
        httpGet.setConfig(requestConfig);
        try{
            response = httpClient.execute(httpGet);
        }catch (Exception e) {}

        //get response into String
        String temp="";
        try{
            HttpEntity entity = response.getEntity();
            temp= EntityUtils.toString(entity,"UTF-8");
        }catch (Exception e) {}

        return temp;
    }

    public static String post(String url, String json){
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = new DefaultHttpClient();
            httpPost = new HttpPost(url);

            StringEntity entity = new StringEntity(json,"utf-8");//解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity);
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }
    /**
     * 读取request流
     * @param request
     * @return
     */
    public static String readReqStr(HttpServletRequest request)
    {
        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try
        {
            reader = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line);
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (null != reader)
                {
                    reader.close();
                }
            } catch (IOException e)
            {

            }
        }
        return sb.toString();
    }
    public static void main(String[] atgs) throws Exception {
        System.out.println(get("http://10.10.31.253:8080/DrcomSrv/DrcomServlet?business=MDAzCTk3MjgJNjAwMAkyMDE4MDUxMDAwMQ=="));
    }

}
